﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Threading;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using AForge.Imaging;

namespace RescueRobotController.Panels
{
    public partial class CameraPanel : DockContent
    {
        private static VideoCapture my_VideoCapture;//摄像头设备
        private static bool Vopen_flag; //视频打开关闭状态
        Thread Video_thread; //视频播放线程
        private int VideoCapture_id = 0;//摄像头设备号，默认0，可根据下拉框调整

        // 下面的内容是每个面板必须要有的
        // *********** BEGIN ************* //
        // “发送AT指令”事件
        public delegate void ExistATCmdToSendHandler(ATCmd cmd);
        public event ExistATCmdToSendHandler SendATCmd;

        // 处理AT指令
        public void HandleATCmd(ATCmd cmd)
        {
            
        }

        public CameraPanel()
        {
            InitializeComponent();
        }

        private void LoadCamerasName()
        {
            comboCameras.Items.Clear();//清空列表
            foreach (var i in EnumCameraDevices.Devices)
            {
                //打印设备名称
                comboCameras.Items.Add(i);
            }
            if (comboCameras.Items.Count == 0)
            {
                MessageBox.Show("没有搜索到摄像头，请检查连线是否有问题");
            }
            else
            {
                comboCameras.SelectedIndex = 0;//默认移动第一个
            }
        }

        private void CameraPanel_Load(object sender, EventArgs e)
        {
            LoadCamerasName();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!Vopen_flag)
            {
                try
                {
                    my_VideoCapture = new VideoCapture(VideoCapture_id);


                    my_VideoCapture.Set(VideoCaptureProperties.FrameWidth, 1920);//宽度
                         my_VideoCapture.Set(VideoCaptureProperties.FrameHeight, 1080);//高度
                         my_VideoCapture.Set(VideoCaptureProperties.Fps,60);//高度


                    /*     my_VideoCapture.Set(VideoCaptureProperties.FrameWidth, 640);//宽度
                 my_VideoCapture.Set(VideoCaptureProperties.FrameHeight, 360);//高度
                 my_VideoCapture.Set(VideoCaptureProperties.Fps,260);//高度  */

                }
                catch (Exception ex)
                {
                    MessageBox.Show("OpenCV方式打开摄像头异常：" + ex.ToString());
                    return;
                }
                if (my_VideoCapture.IsOpened())
                {
                    Vopen_flag = true;
                    Video_thread = new Thread(Play_Video);
                    Video_thread.Start();
                    pictureBox1.Image = null;
                    button1.Text = "关闭摄像头";
                }
            }
            else
            {
                Vopen_flag = false;
                Video_thread.Abort();
                my_VideoCapture.Release();
                button1.Text = "打开摄像头";
            }
            if (my_VideoCapture.IsOpened())
            {
                Vopen_flag = true;
                Video_thread = new Thread(Play_Video);
                Video_thread.Start();
                pictureBox1.Image = null;
                button1.Text = "关闭摄像头";
            }
            else
            {
                Vopen_flag = false;
                Video_thread.Abort();
                my_VideoCapture.Release();
                button1.Text = "打开摄像头";
            }
        }

        private void Play_Video()
        {
            try
            {
                while (Vopen_flag)
                {

                    Mat v_mat = new Mat();
                    my_VideoCapture.Read(v_mat);

                    // v_mat.Resize(new OpenCvSharp.Size(1,1),fx:pictureBox1.Width/v_mat.Width);

                    int sleepTime = (int)Math.Round(1000 / my_VideoCapture.Fps);

                    Cv2.WaitKey(sleepTime);
                    if (v_mat.Empty())
                    {
                        break;
                    }

                    this.pictureBox1.Invoke(new Action(() =>
                    {
                        Mat resizeImg = new Mat(pictureBox1.Height, pictureBox1.Width, MatType.CV_32F);
                        Cv2.Resize(v_mat, resizeImg, new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height));
                        Bitmap bitmap = BitmapConverter.ToBitmap(resizeImg);//在 pictureBox1上显示
                        pictureBox1.Image = bitmap;
                    }));
                    v_mat.Release();//释放内存

                }
            }
            catch(Exception e)
            {

            }
            
        }

        private void comboCameras_SelectedIndexChanged(object sender, EventArgs e)
        {
            VideoCapture_id = comboCameras.SelectedIndex;
        }

        private void CameraPanel_FormClosing(object sender, FormClosingEventArgs e)
        {
            Vopen_flag = false;
            Video_thread.Abort();
            my_VideoCapture.Release();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
        }
    }
}
